/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormUpdate.java
 *
 * Created on Nov 26, 2011, 9:51:57 AM
 */
package Buku;

import Entity.Buku;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author admin
 */
public class FormUpdate extends javax.swing.JPanel {

    private int jumlahLama;
    private int stokLama;

    /** Creates new form FormUpdate */
    public FormUpdate() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSeparator1 = new javax.swing.JSeparator();
        idBukuLabel = new javax.swing.JLabel();
        idBukuText = new javax.swing.JTextField();
        judulLabel = new javax.swing.JLabel();
        judulText = new javax.swing.JTextField();
        pengarangLabel = new javax.swing.JLabel();
        pengarangText = new javax.swing.JTextField();
        penerbitLabel = new javax.swing.JLabel();
        penerbitText = new javax.swing.JTextField();
        tahunLabel = new javax.swing.JLabel();
        tahunText = new javax.swing.JTextField();
        jumlahLabel = new javax.swing.JLabel();
        jumlahText = new javax.swing.JTextField();
        stokLabel = new javax.swing.JLabel();
        stokText = new javax.swing.JTextField();
        updateButton = new javax.swing.JButton();

        setBackground(new java.awt.Color(102, 102, 255));
        setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "UPDATE", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Tempus Sans ITC", 1, 24))); // NOI18N

        idBukuLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        idBukuLabel.setText("ID Buku");

        idBukuText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                idBukuTextKeyPressed(evt);
            }
        });

        judulLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        judulLabel.setText("Judul Buku");

        judulText.setEditable(false);
        judulText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                judulTextActionPerformed(evt);
            }
        });

        pengarangLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        pengarangLabel.setText("Pengarang");

        pengarangText.setEditable(false);

        penerbitLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        penerbitLabel.setText("Penerbit");

        penerbitText.setEditable(false);

        tahunLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        tahunLabel.setText("Tahun Terbit");

        tahunText.setEditable(false);

        jumlahLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        jumlahLabel.setText("Jumlah");

        jumlahText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jumlahTextKeyReleased(evt);
            }
        });

        stokLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        stokLabel.setText("Stok");

        stokText.setEditable(false);

        updateButton.setFont(new java.awt.Font("Tahoma", 1, 12));
        updateButton.setText("Update");
        updateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(idBukuLabel)
                            .addComponent(judulLabel)
                            .addComponent(pengarangLabel)
                            .addComponent(penerbitLabel)
                            .addComponent(tahunLabel)
                            .addComponent(jumlahLabel)
                            .addComponent(stokLabel))
                        .addGap(67, 67, 67)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(judulText)
                            .addComponent(pengarangText)
                            .addComponent(penerbitText)
                            .addComponent(tahunText)
                            .addComponent(jumlahText)
                            .addComponent(stokText, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
                            .addComponent(idBukuText, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(142, 142, 142)
                        .addComponent(updateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(65, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(idBukuLabel)
                    .addComponent(idBukuText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(judulLabel)
                    .addComponent(judulText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(pengarangLabel)
                    .addComponent(pengarangText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(penerbitLabel)
                    .addComponent(penerbitText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tahunLabel)
                    .addComponent(tahunText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jumlahLabel)
                    .addComponent(jumlahText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(stokText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(stokLabel))
                .addGap(34, 34, 34)
                .addComponent(updateButton)
                .addContainerGap(21, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateButtonActionPerformed

        ControllerUpdate conUpdate = new ControllerUpdate();
        Buku buku = new Buku();

        try {

            String id = idBukuText.getText();
            int jumlah = Integer.parseInt(jumlahText.getText());
            int stok = Integer.parseInt(stokText.getText());

//            stokText.setText(String.valueOf(stokBaru));
            buku.setStok(stok);
            buku.setIdBuku(id);
            buku.setJumlah(jumlah);
            conUpdate.updateBuku(buku);


            JOptionPane.showMessageDialog(null, "Berhasil");


        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Gagal");
        }

    }//GEN-LAST:event_updateButtonActionPerformed

    private void judulTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_judulTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_judulTextActionPerformed

    private void idBukuTextKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_idBukuTextKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            try {
                // TODO add your handling code here:

                Buku buku = ControllerUpdate.search(this.idBukuText.getText());
                if (buku != null) {
                    this.judulText.setText(buku.getJudulBuku());
                    this.pengarangText.setText(buku.getPengarang());
                    this.penerbitText.setText(buku.getPenerbit());
                    this.tahunText.setText("" + buku.getThnTerbit());
                    this.jumlahText.setText("" + buku.getJumlah());
                    this.stokText.setText("" + buku.getStok());
                    jumlahLama = buku.getJumlah();
                    stokLama = buku.getStok();
                }
            } catch (SQLException ex) {
                Logger.getLogger(FormUpdate.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_idBukuTextKeyPressed

private void jumlahTextKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jumlahTextKeyReleased
    int jumlah = Integer.parseInt(jumlahText.getText());
    int selJumlah = jumlah - jumlahLama;
    int stokBaru = stokLama + selJumlah;
    stokText.setText("" + stokBaru);
}//GEN-LAST:event_jumlahTextKeyReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel idBukuLabel;
    private javax.swing.JTextField idBukuText;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel judulLabel;
    private javax.swing.JTextField judulText;
    private javax.swing.JLabel jumlahLabel;
    private javax.swing.JTextField jumlahText;
    private javax.swing.JLabel penerbitLabel;
    private javax.swing.JTextField penerbitText;
    private javax.swing.JLabel pengarangLabel;
    private javax.swing.JTextField pengarangText;
    private javax.swing.JLabel stokLabel;
    private javax.swing.JTextField stokText;
    private javax.swing.JLabel tahunLabel;
    private javax.swing.JTextField tahunText;
    private javax.swing.JButton updateButton;
    // End of variables declaration//GEN-END:variables
}
